package is.ejs.bahamut;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class DiceRollActivity extends Activity {
	// Fastar fyrir setOrientiation() föllin
	// http://developer.android.com/reference/android/widget/LinearLayout.html#VERTICAL
	private static final int VERTICAL = 1;
	private static final int HORIZONTAL = 0;
	
	// Listi af af type og number editboxes, notað í calculate
	List<EditText> typeEds = new ArrayList<EditText>();
	List<EditText> numberEds = new ArrayList<EditText>();
	
	//Fjöldi lína:
	int nrOfLines = 0;
	

	
	SecureRandom generator = new SecureRandom();


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dicerolllayout);
	}
	
	
	public void diceRollCalculate(View view) {
		/**
		 * Reiknirit fyrir reikning og geymslu gagna:
		 * 
		 * 1. Sækja aftasta hlut í lista typeEds (EditBox): type
		 * 2. Sækja aftasta hlut í lista numberEds (EditBox): number
		 * 3. Reikna "number" marga teninga með "type" hliðum
		 * 4. Setja allar þessar útkomur í listan singles (intArray)
		 * 5. Reikna summu allra þessara útkoma og setja í listan sums (integer)
		 * 6. Reikna summu allra summa og setja það í heiltöluna globalSum
		 * 
		 * Reiknirit fyrir birtingu gagna:
		 * 
		 * 1. Búa til LinearLayout sem kallast resultLineContainer
		 * 2. Búa til LinearLayout sem kallast resultLine (parent=resultLineContainer)
		 * 3. Búa til TextView sem kallast singleView (parent=resultLine)
		 * 4. Setja útkomur úr listanum singles á strengjaform og setja inn í singlesView
		 * 5. Búa til Textview sem kallast sumView (parent=resultLine)
		 * 6. Setja útkomuna úr listanum á strengjaform og setja inn í sumView
		 * 7. Bæta resultLine við resultLineContainer
		 * 8. Endurtaka skref 2-7 fyrir hvert einasta eintak í listanum typeEds (fjölda lína)
		 * 9. Búa til TextView sem kallast globalResultLine (parent=resultLineContainer)
		 * 10.Setja globalSuml gildið í globalResultLine
		 * 11.Bæta globalResultLine við resultLineContainer
		 */ 
		
		/** Gögn: */
		
		List<int[]> singlesList = new ArrayList<int[]>();
		int[] sumsList = new int[nrOfLines];
		int globalSum = 0;
		
		for(int i=0; i < nrOfLines; i++){
			int number = Integer.parseInt(numberEds.get(i).getText().toString());
			int type = Integer.parseInt(typeEds.get(i).getText().toString());
			int sum = 0;
			int[] array = new int[number];
	    	for(int j=0; i < number; j++){
	    		int randomInt = generator.nextInt(type);
		    	randomInt+=1;
		    	sum = randomInt;
		    	array[j] = randomInt;
			}
	    	singlesList.add(array);
	    	sumsList[i] = sum;
	    	globalSum+= sum;
		}
		
		/** Birting: */
		
		LinearLayout resultLineContainer = new LinearLayout(this);
		resultLineContainer.setOrientation(VERTICAL);
		resultLineContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	            LayoutParams.WRAP_CONTENT));
		
		for(int i=0; i < nrOfLines; i++) {
			String type = typeEds.get(i).getText().toString();
			String number = numberEds.get(i).getText().toString();
			
			LinearLayout resultLine = new LinearLayout(this);
			resultLine.setOrientation(VERTICAL);
			resultLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			
			TextView typeLabel = new TextView(this);
			typeLabel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			typeLabel.setText(type + "d " + "x " + number);
			
			TextView sumLabel = new TextView(this);
			sumLabel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			sumLabel.setText("Sum: " + sumsList[i]);
			
			TextView singleLabel = new TextView(this);
			singleLabel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			String singlesString = "";
			int[] singlesArray = singlesList.get(i);
			for (int j = 0; j < singlesArray.length; j++ ) {
				singlesString += singlesArray[i] + " - ";
			}
			singleLabel.setText("Singles: " + singlesString);
		}
		
		LinearLayout globalResultLine = new LinearLayout(this);
		globalResultLine.setOrientation(VERTICAL);
		globalResultLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		
		TextView globalSumLabel = new TextView(this);
		globalSumLabel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		globalSumLabel.setText("Global Sum: " + globalSum);
		
	}
	
	public void spawnDiceRollLine(View view) {
		/** Tvær aðferðir til þess að lesa afrita línur og gefa
		 * aðgang að Editboxes. Kann ekki almennilega xml aðferðina.
		 * Vantar að geta sótt tilvísun á Editbox gögnin.
		 * Java aðferðin virkar samt sem áður.
		 */
		// Xml aðferð:
		/*
	    ViewGroup parent = (ViewGroup) findViewById(R.id.diceRollLineContainer);
		View viewClone;
		viewClone = LayoutInflater.from(getBaseContext()).inflate(R.layout.dicerollrecipe, parent, false);
		parent.addView(viewClone);
		*/
		
		//Java aðferð:
		// http://stackoverflow.com/questions/5923587/how-to-get-data-from-each-dynamically-created-edittext-in-android
		nrOfLines++; //hækka fjölda lína
		
		ViewGroup parent = (ViewGroup) findViewById(R.id.diceRollLineContainer);
		LinearLayout diceRollLine  = new LinearLayout(this);
		diceRollLine.setOrientation(VERTICAL);
		diceRollLine.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	            LayoutParams.WRAP_CONTENT));
		
		LinearLayout horizontalContainer = new LinearLayout(this);
		horizontalContainer.setOrientation(HORIZONTAL);
		horizontalContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	            LayoutParams.WRAP_CONTENT));
		diceRollLine.addView(horizontalContainer);
		
		TextView diceRollTypeLabel = new TextView(this);
		diceRollTypeLabel.setText(R.string.diceRollTypeLabel);
		horizontalContainer.addView(diceRollTypeLabel, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		EditText diceRollTypeEdit = new EditText(this);
		horizontalContainer.addView(diceRollTypeEdit, new LinearLayout.LayoutParams(80, LayoutParams.WRAP_CONTENT));
		typeEds.add(diceRollTypeEdit);
		
		TextView diceRollNumberLabel = new TextView(this);
		diceRollNumberLabel.setText(R.string.diceRollNumberLabel);
		horizontalContainer.addView(diceRollNumberLabel, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		EditText diceRollNumberEdit = new EditText(this);
		horizontalContainer.addView(diceRollNumberEdit, new LinearLayout.LayoutParams(80, LayoutParams.WRAP_CONTENT));
		numberEds.add(diceRollNumberEdit);
		
		parent.addView(diceRollLine, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	}
}
